package com.ggh.jinjilive.ui.mine;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.FragmentMineDetailBinding;
import com.ggh.jinjilive.listener.AddOnClickListener1;
import com.ggh.jinjilive.mobile.mine.DiamondMobile;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.mine.adapter.MineDetailAdapter;
import com.ggh.jinjilive.utils.GeneralUtils;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MineDetailFragment extends BaseFragment<FragmentMineDetailBinding> implements OnRefreshListener, OnLoadMoreListener {
    //个人明细
    private String particulars;
    //页数
    private int page = 1;
    //适配器
    private MineDetailAdapter mineDetailAdapter;

    public MineDetailFragment(String particulars){
        this.particulars = particulars;

    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_mine_detail;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        View contentView = binding.getRoot().findViewById(R.id.refreshLayout);
        loadService = LoadSir.getDefault().register(contentView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                onRefresh(binding.refreshLayout);
            }
        });
        loadService.showCallback(LoadingCallback.class);
        onRefresh(binding.refreshLayout);
    }


    @Override
    public void initView() {
        super.initView();
        mineDetailAdapter = new MineDetailAdapter();
        binding.rvGoods.setLayoutManager(new LinearLayoutManager(mContext));
        binding.rvGoods.setAdapter(mineDetailAdapter);
    }

    @Override
    public void initData() {
        super.initData();
        //刷新
        binding.refreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //刷新
        binding.refreshLayout.setOnRefreshListener(this);
        //加载
        binding.refreshLayout.setOnLoadMoreListener(this);
    }


    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        Map<String, Object> params = new HashMap<>();
        page = 1;
        params.put("page",page);
        if(particulars.equals("收到礼物明细")){
            binding.tvValue.setText("礼物价值");
            binding.tvNumber.setText("个数");
            binding.tvGiveGifts.setText("送礼人");

            GeneralUtils.infoGiftDetail(params, new AddOnClickListener1() {
                @Override
                public void onSkipClicked(Object mObject) {
                    List<DiamondMobile> diamondMobileList = (List<DiamondMobile>) mObject;
                    if(null != diamondMobileList && diamondMobileList.size() > 0){
                        loadService.showSuccess();
                        mineDetailAdapter.setAdd(diamondMobileList);
                    }else {
                        loadService.showCallback(EmptyCallback.class);
                    }
                }
            });
        }else if(particulars.equals("直播时长明细")){
            binding.tvValue.setText("开始时间");
            binding.tvNumber.setText("结束时间");
            binding.tvGiveGifts.setText("直播时长");
            GeneralUtils.infoLiveDetail(params, new AddOnClickListener1() {
                @Override
                public void onSkipClicked(Object mObject) {
                    List<DiamondMobile> diamondMobileList = (List<DiamondMobile>) mObject;
                    if(null != diamondMobileList && diamondMobileList.size() > 0){
                        loadService.showSuccess();
                        mineDetailAdapter.setAdd(diamondMobileList);
                    }else {
                        loadService.showCallback(EmptyCallback.class);
                    }
                }
            });
        }
    }


    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        Map<String, Object> params = new HashMap<>();
        page = page + 1;
        params.put("page",page);
        if(particulars.equals("收到礼物明细")){
            GeneralUtils.infoGiftDetail(params, new AddOnClickListener1() {
                @Override
                public void onSkipClicked(Object mObject) {
                    List<DiamondMobile> diamondMobileList = (List<DiamondMobile>) mObject;
                    if(null != diamondMobileList && diamondMobileList.size() > 1){
                        binding.refreshLayout.finishLoadMore();
                        mineDetailAdapter.Add(diamondMobileList);
                    }else {
                        binding.refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                }
            });
        }else if(particulars.equals("直播时长明细")){
            GeneralUtils.infoLiveDetail(params, new AddOnClickListener1() {
                @Override
                public void onSkipClicked(Object mObject) {
                    List<DiamondMobile> diamondMobileList = (List<DiamondMobile>) mObject;
                    if(null != diamondMobileList && diamondMobileList.size() > 1){
                        binding.refreshLayout.finishLoadMore();
                        mineDetailAdapter.Add(diamondMobileList);
                    }else {
                        binding.refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                }
            });
        }
    }

}
